package cn.xiongxf.service.impl;

import cn.xiongxf.domain.UserGrowLog;
import cn.xiongxf.mapper.UserGrowLogMapper;
import cn.xiongxf.service.IUserGrowLogService;
import cn.xiongxf.vo.GrowthValVO;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 成长值记录 服务实现类
 * </p>
 *
 * @author xiongxiaofu
 * @since 2022-08-12
 */
@Service
public class UserGrowLogServiceImpl extends ServiceImpl<UserGrowLogMapper, UserGrowLog> implements IUserGrowLogService {

    /**
     * 根据用户ID获取成长值和等级
     */
    @Override
    public GrowthValVO getGrowthVal(Long userId) {
        // 获取用户的成长值：累加
        List<UserGrowLog> userGrowLogs = selectList(new EntityWrapper<UserGrowLog>().eq("user_id", userId));
        Integer growthVal = 0;
        for (UserGrowLog userGrowLog : userGrowLogs) {
            growthVal += userGrowLog.getScore();
        }
        // 获取成长值对应的等级
        String level = getLevel(growthVal);
        return new GrowthValVO(growthVal, level);
    }

    // 抽取方法：获取成长值对应的等级
    private String getLevel(Integer growthVal) {
        if (growthVal >= 0 && growthVal <= 99) return "青铜";
        if (growthVal >= 100 && growthVal <= 299) return "白银";
        if (growthVal >= 300 && growthVal <= 599) return "黄金";
        if (growthVal >= 600 && growthVal <= 999) return "白金";
        if (growthVal >= 1000 && growthVal <= 1499) return "钻石";
        if (growthVal >= 1500 && growthVal <= 2499) return "大师";
        return "王者";
    }
}
